
Ext.ns('MessageRecView');
MessageRecView = Ext.extend(Ext.Panel, {
	gridPanel : null,
	searchPanel : null,
	receiveStore : null,
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);
		this.initUIComponents();
		MessageRecView.superclass.constructor.call(this, {
					id : 'MessageRecView',
					region: 'center',
					layout : 'border',
					items : [this.searchPanel, this.gridPanel]
				});
		
	},// end of constructor
	initUIComponents : function() {
		this.searchPanel = new KE.SearchPanel({
			        region:'north',
					items : [{
								text : '类型'
							}, {
								name : 'Q_EQH_msgType',
								xtype : 'combo',
								mode : 'local',
								width : 100,
								editable : false,
								triggerAction : 'all',
								store : [['','全部'], ['1', '个人信息'], ['4', '待办任务提醒'], ['5', '系统消息']]
							}, {
								text : '发送人'
							}, {
								xtype : 'textfield',
								name : 'Q_LIKES_sender',
								width: 100
							}, {
								text : '从'
							}, {
								xtype : 'datefield',
								format : 'Y-m-d',
								name : 'Q_GED_sendTime',
								editable : false,
								width: 100
							}, {
								text : '到'
							}, {
								xtype : 'datefield',
								format : 'Y-m-d',
								name : 'Q_LED_sendTime',
								editable : false,
								width: 100
							},{
								text : '查询',
								xtype : 'button',
								iconCls : 'search',
								scope: this,
								handler :this.search
							}, {
								xtype : 'button',
								text : '重置',
								iconCls : 'reset',
								scope: this,
								handler :this.reset					
							}]
				});
		this.topbar = new Ext.Toolbar({
					items : [{
							xtype:'button',
							text:'标记为已读',
							iconCls:'ux-flag-blue',
							scope: this,
							handler: this.setReadFlag
						}, '-', {
							iconCls : 'btn-del',
							text : '删除信息',
							xtype : 'button',
							scope: this,
							handler : this.mutDel
						}]
				});
		// 已收消息
		this.gridPanel = new KE.GridPanel({
					region : 'center',
					tbar : this.topbar,
					title : '已收信息显示',
					id : 'ReceiveMessage',
					url : __ctxPath + '/info/listInMessage.do',
					fields : [{
								name : 'receiveId',
								type : 'int'
							}, {
								name : 'messageId',
								mapping : 'shortMessage.messageId',
								type : 'int'
							}, {
								name : 'msgType',
								mapping : 'shortMessage.msgType',
								type : 'int'
							}, {
								name : 'senderId',
								mapping : 'shortMessage.senderId',
								type : 'int'
							}, {
								name : 'sender',
								mapping : 'shortMessage.sender'
							}, {
								name : 'content',
								mapping : 'shortMessage.content'
							}, {
								name : 'sendTime',
								mapping : 'shortMessage.sendTime'
							}, {
								name : 'readFlag'
							}],
					columns : [{
							header : "状态",
							dataIndex : 'readFlag',
							width : 60,
							renderer : function(value) {
								return value == '1' ? "<img src='" + __ctxPath + "/images/btn/info/email_open.png'/>" : "<img src='" + __ctxPath + "/images/btn/info/email.png'/>";
							}
						}, {
							header : "类别",
							dataIndex : 'msgType',
							width : 60,
							renderer : function(value) {
								if (value == '1') {
									return '<font color="green">个人信息</font>';
								} else if (value == '2') {
									return '<font color="green">日程安排</font>';
								} else if (value == '3') {
									return '<font color="green">计划任务</font>';
								} else if (value == '4') {
									return '<font color="green">代办任务提醒</font>';
								} else if (value == '5') {
									return '<font color="green">系统消息</font>';
								} else {
									return '<font color="green">其他</font>';
								}
							}
						}, {
							header : "发送人",
							dataIndex : 'sender',
							width : 50
						}, {
							header : "内容",
							dataIndex : 'content',
							width : 300
						}, {
							header : "发送时间",
							dataIndex : 'sendTime',
							width : 90
						}, {
							header : '操作',
							dataIndex : 'receiveId',
							width : 120,
							renderer : function(value, metadata, record, rowIndex, colIndex) {
								var replyId = record.data.receiveId;
								var senderId = record.data.senderId;
								var sender = record.data.sender;
								var msgType = record.data.msgType;
								var str = '<button title="删除" value=" " class="btn-del" onclick="MessageRecView.removeReceiveMessage('
										+ replyId + ')">&nbsp;</button>';
								if (msgType == '1') { // 1为个人信息
									str += '&nbsp;&nbsp;&nbsp;&nbsp;<button title="回复" value=" " class="btn-suggest-reply" onclick="' +
											'MessageRecView.reply('+replyId+', '+senderId+', \''+sender+'\')">&nbsp;</button>';
								}
								return str;
							}
						}]
				});
		this.gridPanel.addListener('rowdblclick', this.rowClick);		
	},
	
	rowClick : function(grid, rec) {
		new MessageForm2({
					id : rec.data.messageId
				}).show();
	},
	// 重置查询表单
	reset : function() {
		this.searchPanel.getForm().reset();
		this.searchPanel.down('combo').setValue('');
	},
	// 按条件搜索
	search : function() {
		$search({
					searchPanel : this.searchPanel,
					gridPanel : this.gridPanel
				});
	},
	setReadFlag:function(self){
		var grid = this.gridPanel;
		var selectRecords = grid.getSelectionModel().getSelection();
		if (selectRecords.length == 0) {
			Ext.ux.Toast.msg("信息", "请选择信息！");
			return;
		}
		var ids = [];
		for (var i = 0; i < selectRecords.length; i++) {
			ids.push(selectRecords[i].data.receiveId);
		}
		Ext.Ajax.request({
					url : __ctxPath
							+ '/info/multiReadInMessage.do',
					params : {
						ids : ids
					},
					method : 'post',
					success : function() {
						Ext.ux.Toast.msg('操作信息', '成功标记所选信息为已读！');
						grid.getStore().reload();
					}
	  });
	},//标记为已读
	mutDel:function(){
	    var grid = Ext.getCmp("ReceiveMessage");
		var selectRecords = grid.getSelectionModel().getSelection();
		if (selectRecords.length == 0) {
			Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
			return;
		}
		var ids = [];
		for (var i = 0; i < selectRecords.length; i++) {
			ids.push(selectRecords[i].data.receiveId);
		}
		MessageRecView.removeReceiveMessage(ids);
	}
});


MessageRecView.removeReceiveMessage=function(receiveId){
   var receive = Ext.getCmp('ReceiveMessage');
	Ext.Msg.confirm('删除操作', '你确定要删除该信息吗?', function(btn) {
				if (btn == 'yes') {
					Ext.Ajax.request({
								url : __ctxPath + '/info/multiRemoveInMessage.do',
								params : {
									ids : receiveId
								},
								method : 'post',
								success : function() {
									Ext.ux.Toast.msg('操作信息', '删除信息成功！');
									receive.getStore().reload();
								}
							});
				}
			});
};
//回复
MessageRecView.reply = function(replyId, sendId, sender) {
	var reView1 = Ext.getCmp('MessageManageView');
	reView1.removeAll(true);
	var message = new MessageForm({
		replyId: replyId,
		userId: sendId,
		userFullname: sender
	});
	reView1.add(message);
	reView1.doLayout();
};


/**
 * 发送消息
 */
MessageSendView = Ext.extend(Ext.Panel, {
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);
		this.initUIComponents();
		MessageSendView.superclass.constructor.call(this, {
					id : 'MessageSendView',
					region: 'center',
					layout : 'border',
					items : [this.searchPanel, this.gridPanel]
				});
	},
	initUIComponents : function() {
		this.searchPanel = new KE.SearchPanel({
			        region:'north',
					items : [{
								text : '收信人'
							}, {
								xtype : 'textfield',
								name : 'Q_LIKES_userFullnames',
								width: 100
							}, {
								text : '从'
							}, {
								xtype : 'datefield',
								format : 'Y-m-d',
								name : 'Q_GED_sendTime',
								editable : false,
								width: 100
							}, {
								text : '到'
							}, {
								xtype : 'datefield',
								format : 'Y-m-d',
								name : 'Q_LED_sendTime',
								editable : false,
								width: 100
							}, {
								text : '查询',
								xtype : 'button',
								iconCls : 'search',
								scope : this,
								handler : this.search
							}, {
								text : '清空',
								xtype : 'button',
								iconCls : 'reset',
								scope : this,
								handler :this.reset
							}]
				});
		this.topbar = new Ext.Toolbar({
					items : [{
							iconCls : 'btn-del',
							text : '删除信息',
							xtype : 'button',
							scope: this,
							handler : this.removeSelRs
						}]
				});
		// 发送消息
		this.gridPanel = new KE.GridPanel({
					region : 'center',
					id : 'sendMessage',
					tbar : this.topbar,
					title : '已发信息显示',
					url : __ctxPath + '/info/listShortMessage.do',
					fields : ['messageId','msgType', 'content', 'userFullnames', 'sendTime'],
					columns : [{
								dataIndex : 'messageId',
								hidden: true
							},{
								header : "收信人",
								dataIndex : 'userFullnames',
								width : 150
							}, {
								header : "内容",
								dataIndex : 'content',
								width : 250
							}, {
								header : "发送时间",
								dataIndex : 'sendTime',
								width : 90
							}, {
								text: '管理',
								xtype: 'actioncolumn',
					            width: 100,
					            items: [{
					            	iconCls : 'btn-update',
					                tooltip: '重发',
					                scope: this,
					                handler: function(grid, rowIndex, colIndex) {
					                    var rec = grid.getStore().getAt(rowIndex);
					                    this.reSend(rec)
					                }
					            }]
							}],
					sorters: [{
						property: 'messageId',
						direction: 'DESC'					
					}]
				});
		this.gridPanel.addListener('rowdblclick', this.rowClick);
	},
	// 重置查询表单
	reset : function() {
		this.searchPanel.getForm().reset();
	},
	// 按条件搜索
	search : function() {
		$search({
					searchPanel : this.searchPanel,
					gridPanel : this.gridPanel
				});
	},
	reSend:function(record){
		var grid = Ext.getCmp('sendMessage');
		Ext.Ajax.request({
					url : __ctxPath + '/info/sendShortMessage.do',
					params : {
						userId : record.data.userId + ',',
						content : record.data.content
					},
					method : 'post',
					success : function() {
						Ext.ux.Toast.msg('操作信息', '重发成功！');
						grid.getStore().reload();
					}
				});
	},
	rowClick : function(grid, rec) {
		new InMessageForm2({
					id : rec.data.receiveId
				}).show();
	},
	// 把选中ID删除
	removeSelRs : function() {
		$delGridRs({
					url : __ctxPath + '/info/multiDelShortMessage.do',
					grid : this.gridPanel,
					idName : 'messageId'
				});
	}
});


/**
 * 消息管理
 */
MessageManageView = Ext.extend(Ext.Panel, {
			constructor : function(_cfg) {
				Ext.applyIf(this, _cfg);
				// 初始化组件
				this.initUIComponents();
				// 调用父类构造
				MessageManageView.superclass.constructor.call(this, {
							id : 'MessageManageView',
							region : 'center',
							iconCls : "menu-message",
							title : '我的消息',
							tbar : this.toolbar,
							layout: 'fit',
//							autoHeight : true,
							items : []
						});
				this.addRecPanel(this);
			},// end of constructor

			// 初始化组件
			initUIComponents : function() {
				this.toolbar = new Ext.Toolbar({
							layout : 'column',
							items : [{
									xtype : 'button',
									text : '发送信息',
									iconCls : 'btn-sendM',
									scope: this,
									handler : this.addSendFormPanel
								}, {
									xtype : 'button',
									text : '已发信息',
									iconCls : 'btn-sendMessage',
									scope: this,
									handler : this.addSendPanel
								}, {
									text : '已收信息',
									iconCls : 'btn-receiveMessage',
									scope: this,
									handler : this.addRecPanel
								}]
						});
			},// end of initUIComponents
			addRecPanel : function() {
				this.removeAll(true);
				var recPanel = new MessageRecView();
				this.add(recPanel);
				this.doLayout();
			},
			addSendPanel : function() {
				this.removeAll(true);
				var sendPanel = new MessageSendView();
				this.add(sendPanel);
				this.doLayout();
			},
			addSendFormPanel : function() {
				this.removeAll(true);
				var sendForm = new MessageForm();
				this.add(sendForm);
				this.doLayout();
			}

		});